Skip to content

feat: Hoare logic, TM combinators, encoding, and subroutines#7

Merged
SamuelSchlesinger merged 6 commits into
devfrom
feat/hoare-combinators
Apr 4, 2026
Merged

feat: Hoare logic, TM combinators, encoding, and subroutines#7
SamuelSchlesinger merged 6 commits into
devfrom
feat/hoare-combinators

Conversation

@SamuelSchlesinger

Copy link
Copy Markdown
Owner

Summary

Compositional framework for building and verifying Turing machines, extracted and refined from the UTM development branch.

  • Hoare triples (HoareTime/Hoare) with composition rules for seqTM, ifTM, loopTM, complementTM
  • Three new combinators: sequential composition, conditional branching, and loops — all sorry-free
  • Generic proof infrastructure: simulation lifting (simulation_reachesIn), rewind loops (generic_rewind_loop), frame rules (transitionTape_id)
  • State normalization (TM.normalize): convert any TM to use Fin (Fintype.card Q) states with full behavioral equivalence
  • Binary encoding primitives: Γ.encode/Γ.decode, Nat.toBits/Nat.fromBits with roundtrip correctness
  • Five TM subroutines with HoareTime specs: writeTM, rewindWorkTM, scanRightTM, copyInputToWorkTM, compareWorkTapesTM

All proofs are sorry-free. Only standard Lean axioms (propext, Quot.sound, Classical.choice).

New files

File Lines Role
Hoare/Defs.lean 131 TapePred, HoareTime, Hoare, structural rules
Hoare.lean 354 Composition rules for all combinators
Combinators/Internal/Generic.lean 324 Shared simulation lifting, rewind loops, transition properties, frame rules
Combinators/Internal/Union.lean 1588 unionTM simulation proofs (refactored from Internal.lean)
Combinators/SeqInternal.lean 150 seqTM simulation proofs
Combinators/IfInternal.lean 456 ifTM simulation proofs
Combinators/LoopInternal.lean 464 loopTM simulation proofs
Encoding.lean 205 State normalization, binary encoding, enumeration
Subroutines.lean 278 Five composable TM building blocks
Subroutines/Internal.lean 408 HoareTime proofs for subroutines

Time bounds

Rule Time
seqTM_hoareTime b₁ + 1 + b₂
complementTM_hoareTime b + p_bound + 4
ifTM_hoareTime b_test + p_bound + max b_then b_else + 5
loopTM_hoareTime (k + 1) * b_iter
writeTM_hoareTime B + 3
rewindWorkTM_hoareTime B + 2

Test plan

  • lake build passes with zero errors and zero warnings
  • All diagnostics clean across all 11 new/modified files
  • lean_verify confirms only standard axioms on all key theorems
  • Zero sorry's across entire codebase
  • No set_option linter suppressions

…roof infrastructure

Add three new TM combinators for compositional machine construction:

- seqTM: sequential composition (tm₁ then tm₂), time t₁ + 1 + t₂
- ifTM: conditional branching (test → rewind → branch), with then/else
- loopTM: loop until test condition, with body + test + rewind cycle

Refactor proof internals into modular structure:
- Internal/Generic.lean: reusable simulation_reachesIn and
  generic_rewind_loop infrastructure
- Internal/Union.lean: unionTM-specific proofs (extracted from Internal.lean)
- SeqInternal, IfInternal, LoopInternal: per-combinator proof modules

All proofs are sorry-free. The generic rewind loop pattern captures the
recurring output-head-rewind used by complement, if, and loop combinators.
Introduce a Hoare triple framework for Turing machines:

- TapePred: predicates on (input, work, output) tape configurations
- HoareTime: time-bounded Hoare triple {pre} tm {post} [≤ bound]
- Hoare: unbounded variant for pure correctness
- Structural rules: consequence, weakening, strengthening, monotonicity

Composition rules for all combinators:
- seqTM_hoareTime: sequential composition with intermediate predicate
- complementTM_hoareTime: output bit flipping with head bound
- ifTM_hoareTime: conditional branching with AllTapesWF tracking
- loopTM_hoareTime: loop invariant rule with variant-based termination

AllTapesWF tracks well-formedness (cell 0 = ▷, cells ≥ 1 ≠ ▷) as a
stability invariant preserved through tape transitions.
…suppression

- Extract `transitionTape`/`transitionInput` and their property lemmas
  (`_cells`, `_head_ge`, `_head_bound`) into Generic.lean as the single
  source of truth. These were previously duplicated as
  `seqTransitionTape`, `ifTransitionTape`, and `loopTransitionTape`.
- Remove `set_option linter.unusedSimpArgs false` from Hoare.lean (the
  underlying simp calls were already clean).
- Rename `AllTapesWF.ifTransition` → `AllTapesWF.transition` since the
  property is combinator-agnostic.

Net: -64 lines, zero duplication of the phase-transition tape operations.
… and TM building blocks

Extract general-purpose infrastructure from the UTM development:

**Encoding.lean**: State normalization and binary encoding primitives.
- `TM.normalize`: convert any TM to use `Fin (Fintype.card Q)` states,
  with full behavioral equivalence (`normalize_decidesInTime`)
- `Γ.encode`/`Γ.decode`, `Γw.encode`, `Dir3.encode`: 2-bit symbol/direction
  encodings with roundtrip correctness
- `Nat.toBits`/`Nat.fromBits`: fixed-width big-endian binary
- `allΓ`, `allΓFuncs`: canonical enumeration of tape symbols

**Subroutines.lean**: Five composable TM building blocks.
- `writeTM sym`: write symbol to output cell 1 and halt
- `rewindWorkTM idx`: rewind work tape to cell 1
- `scanRightTM idx`: scan work tape right until blank
- `copyInputToWorkTM idx`: copy input tape to work tape
- `compareWorkTapesTM i j`: compare two work tapes cell by cell

**Subroutines/Internal.lean**: HoareTime specifications.
- `writeTM_hoareTime`: time B+3, postcondition output cell 1 = sym.toΓ
- `rewindWorkTM_hoareTime`: time B+2, postcondition head = 1
- `rewindWorkTM_rich_hoareTime`: preserves arbitrary predicate P through rewind

**Generic.lean additions**: Frame rules for composition.
- `transitionTape_id`: transitionTape is identity when tape reads non-▷
- `transitionInput_id`: same for input tape
- `rightOfStart_allIdle` made public (needed by subroutines)
- Add time-bound formulas and tape transition explanation to Hoare.lean
  module docstring
- Add docstrings to all config wrapping functions (phase1Wrap, phase2Wrap,
  ifTestWrap, ifThenWrap, ifElseWrap, loopBodyWrap, loopTestWrap)
@SamuelSchlesinger SamuelSchlesinger merged commit b6a0916 into dev Apr 4, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant